#!/bin/python3

import re

matrix = '/home/matrix/Maxwell/hypre_CSR_1.M'
save_matrix = '/home/matrix/Maxwell/jxpamg_CSR_1.M'
matrix_1 = "/home/matrix/Maxwell/hypre_CSR_1.M"
save_matrix_1 = "/home/matrix/Maxwell/jxpamg_CSR_1.M"
matrix_type = 'mtx'
statr_line_number = 2
row_first = 1
col_first = 1

i = 1
numdata = 0
row_ptr = []
col = []
value = []
for line in open(matrix): 
    line = line.strip("\n")
    #print(line)
    #xx = input()
    #line = re.split(r"[ ]+", line)
    #print(line)
    if i == 1:
        numrow = int(line)
    if i == 2:    
        base_file = int(line)
    if i >= statr_line_number:
        if i < statr_line_number + numrow: 
            row_ptr.append(int(line) - base_file)
        if i == statr_line_number + numrow:
            row_ptr.append(int(line) - base_file)
            numdata = int(line)
        if numdata != 0 and i > statr_line_number + numrow and i < statr_line_number + numrow + numdata:
            col.append(int(line) - base_file)
        if numdata != 0 and i >= statr_line_number + numrow + numdata: 
            value.append(line)
    i = i + 1

print("\nread matrix success!\n")

with open(save_matrix, "w+" ) as fo:
    fo.write(str(numrow)+"\n")
    for i in range(0, numrow + 1):
        fo.write(str(row_ptr[i])+"\n")
    for i in range(0, len(col)):
        fo.write(str(col[i])+"\n")
    for i in range(0, len(value)):
        fo.write(str(value[i])+"\n")

print("\ntrans to csr success!\n")